/*
Abstract:
  Analyze by strings functionality implementation

Last changed:
  $Id: analyze_strings.cpp 55 2009-03-26 10:54:52Z vitamin.caig $

Author:
  (C) Vitamin/CAIG/2001

  This file is the part of Textator project
*/

#include "analyzer.h"

namespace Analyzer
{
  void ByStrings(const StringMap& source, EntriesMap& result, StringMap& remainder, Statistic::Aliasing& statistic)
  {
    statistic = Statistic::Aliasing();
    result.clear();
    remainder.clear();
    StringMap reverted;
    for (StringMap::const_iterator it = source.begin(), lim = source.end(); it != lim; ++it)
    {
      if (IsSpecialToken(it->first))
      {
        result.insert(EntriesMap::value_type(it->first, Entry(Entry::GLOBAL, Token::TEXT, it->second)));
      }
      else
      {
        std::pair<StringMap::iterator, bool> ins(reverted.insert(StringMap::value_type(it->second, it->first)));
        if (ins.second)//inserted
        {
          remainder.insert(*it);//as is
        }
        else
        {
          result.insert(EntriesMap::value_type(it->first, Entry(Entry::GLOBAL, Token::VARIABLE, ins.first->second)));
          ++statistic.AliasesCount;
          statistic.AliasedSymbols += it->second.size();
        }
      }
    }
  }
}
